package com.chen.helper;


import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chen.helper.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * RecycleView 辅助类:,不刷新
 * T: 具体的javaBean
 */

abstract class AbstractRecycleViewHelper<T> {
    private RecyclerView rv;
    BaseQuickAdapter<T, BaseViewHolder> adapter;


    AbstractRecycleViewHelper(int resId, RecyclerView rv) {
        if (resId == 0 || rv == null) {
            return;
        }

        adapter = new BaseQuickAdapter<T, BaseViewHolder>(resId) {
            @Override
            protected void convert(BaseViewHolder helper, T item) {
                setItemData(helper, item);
            }
        };

        this.rv = rv;
        this.rv.setAdapter(adapter);
        //设置默认的LayoutManager
        if (rv.getLayoutManager() == null) {
            this.rv.setLayoutManager(new WrapContentLinearLayoutManager(rv.getContext()));
        }
        adapter.setEmptyView(R.layout.item_emptyview_show, rv);
        this.rv.setHasFixedSize(true);//默认true
        //RecyclerView局部刷新动画屏蔽,try to  setNull()
        ((SimpleItemAnimator) rv.getItemAnimator()).setSupportsChangeAnimations(false);
    }


    /**
     * 通过数据处理ui
     *
     * @param helper
     * @param item
     */
    protected abstract void setItemData(BaseViewHolder helper, T item);

    /**
     * @see AbstractRecycleViewHelper#setListData(List, boolean)
     */
    public void setListData(List<T> itemData) {
        setListData(itemData, true);
    }




    /**
     * @param itemData  传入的数据
     * @param isClear   true: 清除之前的数据 ,false :追加
     */
    public void setListData(List<T> itemData, boolean isClear) {
        List<T> empty = new ArrayList<>();
        itemData = itemData == null ? empty : itemData;
        if (isClear) {
            adapter.getData().clear();
        }
        adapter.addData(itemData);



    }

    /**
     * notifyDataSetChanged
     */
    public void refreshList() {
        adapter.notifyDataSetChanged();
    }

    public void addItem(T t) {
        if (t != null) {
            adapter.addData(t);
        }

    }

    public void addItem(T t, int index) {
        if (t != null) {
            adapter.addData(index, t);
        }
    }

    /**
     * 增加到第一个位置
     *
     * @param t
     */
    public void add2FirstItem(T t) {
        addItem(t, 0);
    }

    public void changeItem(int index, T t) {
        if (t != null && index >= 0 && index < adapter.getData().size()) {
            adapter.setData(index, t);
        }

    }

    /**
     *
     * @param t item 的某个字段 需要更新
     */
    public void changeItem( T t) {
        
        int index = adapter.getData().indexOf(t);
        changeItem(index,t);

    }

    public void removeItem(T t) {
        if (t != null) {
            removeItem(adapter.getData().indexOf(t));
        }
    }

    public void removeItem(int index) {
        if (index >= 0 && index < adapter.getData().size()) {
            adapter.remove(index);
        }
    }


    /**
     * 错误处理
     */
    public void setError() {

    }

    public BaseQuickAdapter<T, BaseViewHolder> getAdapter() {
        return adapter;
    }


    public AbstractRecycleViewHelper<T> addHeadView(View view) {
        adapter.addHeaderView(view);
        return this;
    }

    public AbstractRecycleViewHelper<T> addFootView(View view) {
        adapter.addFooterView(view);
        return this;
    }

}
